import * as types from './types'

export const state = {
  frozenRepayment: {
    repays: [],
    loading: false,
    conditions: {
      mobile: ''
    },
    pageSize: 10,
    total: 0,
    curPage: 1,
    details: [],
    dialogVisible: false,
    dialogLoading: false
  }
}

export const getters = {
  frozenRepayment: state => state.frozenRepayment
}

export const actions = {
  getFrozenRepays({
    commit
  }, payload) {
    commit(types.GET_FROZEN_REPAYS, payload);
  },
  queryRepayDetails({
    commit
  }, payload) {
    commit(types.QUERY_REPAY_DETAILS, payload);
  },
  confirmFrozenRepay({
    commit
  }, payload) {
    commit(types.CONFIRM_FROZEN_REPAY, payload);
  }
}

export const mutations = {
  /**
   * 获取还款冻结列表
   * 
   * @param {any} state 
   * @param {any} {
   *     vm,
   *     page
   *   } 
   */
  [types.GET_FROZEN_REPAYS](state, {
    vm,
    page
  }) {
    state.frozenRepayment.loading = true;
    const params = {
      limit: state.frozenRepayment.pageSize,
      mobile: state.frozenRepayment.conditions.mobile
    };
    params.state = page ? page : state.frozenRepayment.curPage;

    vm.$ajax.get('/repay/showRepayFrozeLoan', {
        params: params
      })
      .then(res => {
        let data = res.data;
        if (data && data.status == vm.$appConfig.success) {
          state.frozenRepayment.repays = data.list;
          state.frozenRepayment.total = data.total;
          state.frozenRepayment.pageSize = data.pageSize;
          state.frozenRepayment.curPage = data.curPage;
          state.frozenRepayment.loading = false;
        } else {
          state.frozenRepayment.repays = [];
          state.frozenRepayment.loading = false;
        }
      })
      .catch(err => {
        state.frozenRepayment.repays = [];
        state.frozenRepayment.loading = false;

        vm.$notify.error({
          title: '失败',
          message: '获取还款冻结列表失败'
        });
        console.error(err);
      });
  },
  /**
   * 查询还款明细
   * 
   * @param {any} state 
   * @param {any} {
   *     vm,
   *     row
   *   } 
   */
  [types.QUERY_REPAY_DETAILS](state, {
    vm,
    row
  }) {
    state.frozenRepayment.dialogVisible = true;
    state.frozenRepayment.dialogLoading = true;
    vm.$ajax.get('/repay/queryRepayDetl', {
        params: row
      })
      .then(res => {
        let data = res.data;
        if (data) {
          state.frozenRepayment.details = data;
        } else {
          state.frozenRepayment.details = [];
        }
        state.frozenRepayment.dialogLoading = false;
      })
      .catch(err => {
        state.frozenRepayment.details = [];
        state.frozenRepayment.dialogLoading = false;
        vm.$notify.error({
          title: '提示',
          message: '获取还款详情列表失败'
        });
      });
  },
  /**
   * 确认还款冻结
   * 
   * @param {any} state 
   * @param {any} {
   *     vm,
   *     index,
   *     row
   *   } 
   */
  [types.CONFIRM_FROZEN_REPAY](state, {
    vm,
    index,
    row
  }) {
    window.open(`${vm.$appConfig.domain}/repay/frozeRepayAmount?${vm.$postParams(row)}`);
  }
}